package bb.lanxing.lib.devices.ble.dfu;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.Context;
import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.lib.devices.common.DeviceLogger;
import bb.lanxing.lib.devices.core.ble.IGattClient;
import bb.lanxing.lib.devices.core.dfu.AbsDfuAdapter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class DfuAdapter extends AbsDfuAdapter implements ConnectionListener {
    private final UUID DFU_SERVICE;
    private final UUID DFU_WRITER;
    private final String TAG;

    public DfuAdapter(Context context, IGattClient iGattClient) {
        super(context, iGattClient);
        this.DFU_SERVICE = UUID.fromString("0000FFF0-0000-1000-8000-00805f9b34fb");
        this.DFU_WRITER = UUID.fromString("0000FFF1-0000-1000-8000-00805f9b34fb");
        this.TAG = "DfuAdapter";
        iGattClient.registerConnectionListener(this);
    }

    @Override
    public void enterDfuMode() {
        if (isDfuOn() && !isServicesDiscovered()) {
            notifyDufStateChanged();
            return;
        }
        try {
            setDfuModeState(1);
            _enterDfuMode();
        } catch (IllegalStateException e) {
            e(e);
            notifyDufStateChanged();
        }
    }

    @Override
    public void onConnectionStateChanged(SmartDevice smartDevice, int i, int i2) {
        if (isDfuTurningOn() && i == DeviceManager.STATE_DISCONNECTED) {
            setDfuModeState(2);
        }
    }

    private void _enterDfuMode() {
        d("Try to enter dfu mode");
        if (!isServicesDiscovered()) {
            throw new IllegalStateException("not connected");
        }
        BluetoothGatt bluetoothGatt = this.client.getBluetoothGatt();
        byte[] bArr = null;
        bArr = "DFU".getBytes(StandardCharsets.UTF_8);
        if (bArr == null) {
            throw new IllegalStateException("parse directive err");
        }
        BluetoothGattService service = bluetoothGatt.getService(this.DFU_SERVICE);
        if (service == null) {
            throw new IllegalStateException("Not fount dfu service");
        }
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(this.DFU_WRITER);
        if (characteristic == null) {
            throw new IllegalStateException("Not fount dfu characteristic");
        }
        characteristic.setValue(bArr);
        if (!bluetoothGatt.writeCharacteristic(characteristic)) {
            throw new IllegalStateException("Failed to write characteristic");
        }
    }

    private boolean isServicesDiscovered() {
        return this.client.getConnectionState() == 8;
    }

    protected void e(Throwable th) {
        DeviceLogger deviceLogger = DeviceContext.getDeviceLogger();
        if (deviceLogger != null) {
            deviceLogger.e(this.TAG, th);
        }
    }

    protected void d(String str) {
        DeviceLogger deviceLogger = DeviceContext.getDeviceLogger();
        if (deviceLogger != null) {
            deviceLogger.d(this.TAG, str);
        }
    }
}
